home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / VideoStreamV1.0 / Source / mpegDecodeSrc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  5.7 KB  |  279 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include "video.h"
  22. #include <sys/types.h>
  23. #include <signal.h>
  24. #include <netinet/in.h>
  25. #include "util.h"
  26.  
  27. /* Define buffer length. */
  28.  
  29. #define BUF_LENGTH 80000
  30.  
  31. /* Function return type declarations */
  32. void usage();
  33.  
  34. /* External declaration of main decoding call. */
  35.  
  36. extern VidStream *mpegVidRsrc();
  37. extern VidStream *NewVidStream();
  38.  
  39. /* Global file pointer to incoming data. */
  40. FILE *input;
  41.  
  42. /* End of File flag. */
  43. static int EOF_flag = 0;
  44.  
  45. /* Loop flag. */
  46. int loopFlag = 0;
  47.  
  48. /* Setjmp/Longjmp env. */
  49. jmp_buf env;
  50.  
  51.  
  52. /*
  53.  *--------------------------------------------------------------
  54.  *
  55.  * get_more_data --
  56.  *
  57.  *    Called by correct_underflow in bit parsing utilities to
  58.  *      read in more data.
  59.  *
  60.  * Results:
  61.  *    Input buffer updated, buffer length updated.
  62.  *      Returns 1 if data read, 0 if EOF, -1 if error.
  63.  *
  64.  * Side effects:
  65.  *      None.
  66.  *
  67.  *--------------------------------------------------------------
  68.  */
  69.  
  70. int 
  71. get_more_data(buf_start, max_length, length_ptr, buf_ptr)
  72.      unsigned int *buf_start;
  73.      int max_length;
  74.      int *length_ptr;
  75.      unsigned int **buf_ptr;
  76. {
  77.   
  78.   int length, num_read, i, request;
  79.   unsigned char *buffer, *mark;
  80.   unsigned int *lmark;
  81.  
  82.   if (EOF_flag) return 0;
  83.  
  84.   length = *length_ptr;
  85.   buffer = (unsigned char *) *buf_ptr;
  86.  
  87.   if (length > 0) {
  88.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  89.     mark = ((unsigned char *) (buf_start + length));
  90.   }
  91.   else {
  92.     mark = (unsigned char *) buf_start;
  93.     length = 0;
  94.   }
  95.  
  96.   request = (max_length-length)*4;
  97.   
  98.   num_read = fread( mark, 1, request, input);
  99.  
  100.   if (num_read < 0) {
  101.     return -1;
  102.   }
  103.   else if (num_read == 0) {
  104.     *buf_ptr = buf_start;
  105.     
  106.     /* Make 32 bits after end equal to 0 and 32
  107.        bits after that equal to seq end code
  108.        in order to prevent messy data from infinite
  109.        recursion.
  110.     */
  111.  
  112.     *(buf_start + length) = 0x0;
  113.     *(buf_start + length+1) = SEQ_END_CODE;
  114.  
  115.     EOF_flag = 1;
  116.     return 0;
  117.   }
  118.  
  119.   lmark = (unsigned int *) mark;
  120.  
  121.   num_read = num_read/4;
  122.  
  123.   for (i=0; i<num_read; i++) {
  124.     *lmark = htonl(*lmark);
  125.     lmark++;
  126.   }
  127.  
  128.   *buf_ptr = buf_start;
  129.   *length_ptr = length + num_read;
  130.  
  131.   return 1;
  132. }
  133.  
  134. /*
  135.  *--------------------------------------------------------------
  136.  *
  137.  * int_handler --
  138.  *
  139.  *    Handles Cntl-C interupts..
  140.  *
  141.  * Results:
  142.  *    None.
  143.  *
  144.  * Side effects:
  145.  *    None.
  146.  *
  147.  *--------------------------------------------------------------
  148.  */
  149. void
  150. int_handler()
  151. {
  152.   fprintf(stderr, "Interrupted!\n");
  153.   if (curVidStream != NULL)
  154.     DestroyVidStream(curVidStream);
  155.   exit(1);
  156. }
  157.  
  158. /* global - writes binary data to stdout if set */
  159. int writeToStdout;
  160.  
  161. /*
  162.  *--------------------------------------------------------------
  163.  *
  164.  * main --
  165.  *
  166.  *    Parses command line, starts decoding and displaying.
  167.  *
  168.  * Results:
  169.  *    None.
  170.  *
  171.  * Side effects:
  172.  *    None.
  173.  *
  174.  *--------------------------------------------------------------
  175.  */
  176.  
  177. void main(argc, argv)
  178. int argc;
  179. char **argv;
  180. {
  181.     static VidStream *theStream;
  182.     int mark;
  183.     int i;
  184.     
  185.     writeToStdout = 1;
  186.     mark = 1;
  187.     argc--;
  188.     
  189.     input = stdin;
  190.     
  191.     while (argc) {
  192.         if (strcmp(argv[mark], "-nop") == 0) {
  193.             TogglePFlag();
  194.             argc--; mark++;
  195.         } 
  196.         else if (strcmp(argv[mark], "-nob") == 0) {
  197.             ToggleBFlag();
  198.             argc--; mark++;
  199.         } 
  200.         else if (strcmp(argv[mark], "-toFile") == 0) {
  201.             writeToStdout = 0;
  202.             argc--; mark++;
  203.         } 
  204.         else if (strcmp(argv[mark], "-loop") == 0) {
  205.             argc--; mark++;
  206.             loopFlag = 1;
  207.         }
  208.         else 
  209.         if (argv[mark][0] == '-') {
  210.             fprintf(stderr, "Un-recognized flag %s\n",argv[mark]);
  211.             usage(argv[0]);
  212.         }
  213.         else {
  214.             input = fopen(argv[mark], "r");
  215.             if (input == NULL) {
  216.                 fprintf(stderr, "Could not open file %s\n", argv[mark]);
  217.                 usage(argv[0]);
  218.             }
  219.             argc--; mark++;
  220.         }
  221.     }
  222.     
  223.     signal(SIGINT, int_handler);
  224.     
  225.     init_tables();
  226.     InitColorDither();
  227.     
  228.     if (setjmp(env) != 0) {
  229.         DestroyVidStream(theStream);
  230.         rewind(input);
  231.         EOF_flag = 0;
  232.         curBits = 0;
  233.         bitOffset = 0;
  234.         bufLength = 0;
  235.         bitBuffer = NULL;
  236.         totNumFrames = 0;
  237.     }
  238.     
  239.     theStream = NewVidStream(BUF_LENGTH);
  240.     mpegVidRsrc(0, theStream);
  241.     i = 1;  
  242.     
  243.     realTimeStart = ReadSysClock();
  244.     
  245.     while (1) {    /* Ctl-C to exit if in loop mode */
  246.         mpegVidRsrc(0, theStream);
  247.     }
  248. }
  249.  
  250.  
  251. /*
  252.  *--------------------------------------------------------------
  253.  *
  254.  * usage --
  255.  *
  256.  *    Print mpeg_play usage
  257.  *
  258.  * Results:
  259.  *    None.
  260.  *
  261.  * Side effects:
  262.  *    exits with a return value -1
  263.  *
  264.  *--------------------------------------------------------------
  265.  */
  266.  
  267. void
  268. usage(s)
  269. char *s;    /* program name */
  270. {
  271.     fprintf(stderr, "Usage:\n");
  272.     fprintf(stderr, "mpeg_play\n");
  273.     fprintf(stderr, "          [-nob]\n");
  274.     fprintf(stderr, "          [-nop]\n");
  275.     fprintf(stderr, "          [-loop]\n");
  276.     fprintf(stderr, "          file_name\n");
  277.     exit (-1);
  278. }
  279.